package com.shuqi.activity.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.shuqi.application.ShuqiApplication;
import com.shuqi.common.Constant;
import com.shuqi.common.PayTheme;
import com.shuqi.common.utils.Log4an;
import com.shuqi.controller.R;
import com.shuqi.model.bean.BookContentInfo;

/**
 * 目录Adapter
 * 
 * @author Jianda.Yang
 * 
 * @Date 2014-1-4 下午12:34:09
 */
public class CatalogAdapter extends BaseAdapter {
    private LayoutInflater inflater;
    public List<BookContentInfo> mListInfos;
    private int mSelection = 0;
    private int mTextFontColor;
    private int mSelectorTextFontColor;
    private int mImageID;
    public CatalogAdapter(Context context, List<BookContentInfo> liInfos) {
        this.inflater = LayoutInflater.from(context);
        mListInfos = liInfos;
        setTheme();
    }

    public void setData(List<BookContentInfo> liInfos) {
        mListInfos = liInfos;
    }

    public void setSelection(int selection) {
        Log4an.e("yjd", "local,mSelection:"+mSelection);
        mSelection = selection;
    }

    @Override
    public int getCount() {
        return mListInfos == null ? 0 : mListInfos.size();
    }

    @Override
    public Object getItem(int position) {
        return mListInfos == null ? null : mListInfos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    MyHolder holder ;
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
        if (convertView != null) {
            view = convertView;
        } else {
            view = inflater.inflate(R.layout.list_item, parent, false);
        }
        MyHolder holder = (MyHolder) view.getTag();
        if (holder == null) {
            holder = new MyHolder();
            holder.tv1 = (TextView) view.findViewById(R.id.textView1);
            holder.img = (ImageView) view.findViewById(R.id.imgView_itemlayout_listview3);
            view.setTag(holder);
        }
        
        holder.tv1.setText(mListInfos.get(position).getCurChapterName());
        if (position == mSelection) {
            holder.img.setVisibility(View.VISIBLE);
            holder.img.setBackgroundResource(mImageID);
            holder.tv1.setTextColor(mSelectorTextFontColor);
            if(PayTheme.getCurrentTheme() == 3){
                holder.tv1.setTextColor(0xff1ae195);
            }
        } else {
            holder.img.setVisibility(View.GONE);
            holder.tv1.setTextColor(mTextFontColor);
        }
        return view;
    }

    public void setTheme() {
        int themeIndex = PayTheme.getCurrentTheme(ShuqiApplication.getContext());
        mTextFontColor = Constant.READ_FONT_COLORS[themeIndex];
        if(PayTheme.PAY_THEME == themeIndex){//夜间
            mSelectorTextFontColor = 0xff0c6b47;
            mImageID=Constant.CATALOG_BOOKMARK_IMAGE[0];
        }else{
            mSelectorTextFontColor = 0xff14be7d;
            mImageID=Constant.CATALOG_BOOKMARK_IMAGE[1];
        }
    }

    private static class MyHolder {
        public TextView tv1;
        public ImageView img;
    }
}
